import { v4 as uuid } from 'uuid';
import update from './atom/db/update';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	exam_id: string;
	exam_name: string;
	start_time: string;
	end_time: string;
	pm_start_time: string;
	am_start_time: string;
	creater: string;
}

async function atom(msg: Message) {
	const systemid = '5981';

	const query_obj = query(action_id, session_id, spaceid, systemid);
	query_obj.prepare('exam', ['_id', 'exam_name', 'exam_id', 'start_date', 'finish_date', 'days', 'am_start_time', 'pm_start_time'], { exam_id: msg.exam_id }, 1, 1);
	const [q_data] = await query_obj.exec();

	const { prepare, exec } = update(action_id, session_id, spaceid, systemid);

	const start_date = new Date(msg.start_time).getTime();
	const finish_date = new Date(msg.end_time).getTime();
	const days = (finish_date - start_date) / 1000 / 60 / 60 / 24 + 1;

	const rowdata: any = q_data[0];
	rowdata.exam_name = msg.exam_name;
	rowdata.start_date = start_date;
	rowdata.finish_date = finish_date;
	rowdata.days = days;
	rowdata.am_start_time = msg.am_start_time;
	rowdata.pm_start_time = msg.pm_start_time;

	prepare('exam', [rowdata]);
	const ret = await exec();
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
};
